/**
 * 消毒记录
 * @constructor
 */
function Disinfect() {
}

Disinfect.init = function () {
    let vm = new Vue({
        el: "#customModule",
        data: {
            //实体
            disinfectId:"",
            disinfectTime:"",
            disinfectionPlace:"",
            disinfectName:"",
            dosage:"",
            method:"",
            otherMethod:"",
            remark:"",
            //搜索值
            disinfectNameCondition: "",
            timeIntervale:"",
            //附加属性
            clickState: "",
            roleID: "",
            isAdd: "true",
            roleList: ""
        },
        created() {
            //日期区间选择
            laydate.render({
                elem: '#disinfectTime',
                theme: 'molv',
                type:'date',
                done:function (value) {
                    vm.disinfectTime = value;
                }
            });
            laydate.render({
                elem: '#search_TimeIntervale',
                range: true,
                done:function (value) {
                    vm.timeIntervale = value;
                }
            });
            this.roleID = $.cookie('RoleIds');
            self = this;
            window.changeState = self.changeState;
            window.edit = self.edit;
            window.del = self.del;
        },
        mounted() {
            this.initBootStrapSource();
        },
        computed: {},
        watch: {
            method (newName, oldName) {
                vm.changeMethod();
            }
        },
        methods: {
            initBootStrapSource() {
                $('#tableList').bootstrapTable({
                    url: '/disinfect/tableSearch',
                    method: 'post',
                    toolbar: "#toolbar",
                    sidePagination: 'server',
                    dataField: "data",
                    striped: true,
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 25, 50, 100],
                    sortable: true,
                    sortName: 'createDateTime',
                    sortOrder: "desc",
                    search: false,
                    showRefresh: true,
                    showColumns: true,
                    showExport: true,
                    showToggle: true,
                    queryParamsType: 'undefined',
                    queryParams: function queryParams(params) {
                        return {
                            pageSize: params.pageSize,      //页面大小
                            pageIndex: params.pageNumber,   //页码
                            sort: params.sortName,      //排序列名
                            sortOrder: params.sortOrder, //排位命令（desc，asc）
                            disinfectName:$("#disinfectNameCondition").val(),
                            timeInterval: $('#search_TimeIntervale').val(), // 请求时向服务端传递的参数
                        };
                    },
                    onLoadSuccess: function (data) {
                        //Help.alertSuccMessage("数据加载完毕");
                    },
                    onLoadError: function (status) {
                        // Help.alertErrorMessage("数据加载出错");
                    },
                    columns: [
                        {
                            title: '序号',//标题  可不加
                            align: "center",
                            width: 40,
                            sortable: true,
                            formatter: function (value, row, index) {
                                var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                                var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                                return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                            }
                        },
                        {field: 'enterpriseName', title: '企业名称', align: 'center'},
                        {field: 'disinfectTime', title: '消毒日期', align: 'center'},
                        {field: 'disinfectionPlace', title: '消毒场所', align: 'center'},
                        {field: 'disinfectName', title: '消毒品名称', align: 'center'},
                        {field: 'dosage', title: '用药剂量', align: 'center'},
                        {field: 'method', title: '消毒方式', align: 'center'},
                        {field: 'userName', title: '上报人', align: 'center'},
                        {field: 'createTime', title: '上报日期', align: 'center'},
                        {field: 'remark', title: '备注', align: 'center'},
                        {
                            title: '操作', align: 'center',
                            formatter: function (value, row, index) {
                                console.log("row",row)
                                let optionHtml="";
                                optionHtml = optionHtml+"<a class=\"btn btn-success btn-xs\"  href=\"javascript:void(0)\"  onclick='edit(" + JSON.stringify(row) + ",\"edit\")' title=\"修改\" ><i class=\"fa fa-edit\"></i>修改</a>";
                                optionHtml = optionHtml + " <a class=\"btn btn-danger btn-xs\" href=\"javascript:void(0)\"  onclick='del(" + JSON.stringify(row) + ",\"delete\")'  title=\"删除\"><i class=\"fa fa-remove\"></i>删除</a>"
                                return optionHtml;
                            }
                        }
                    ]
                });
            },
            /**
             * 查询
             */
            search() {
                vm.refresh();
            },
            /**
             * 重置
             */
            reset() {
                vm.disinfectNameCondition = "";
                $("#disinfectNameCondition").val("");
                vm.timeIntervale="";
                $("#search_TimeIntervale").val("");
                vm.refresh();
            },
            /**
             * 编辑
             * @param data
             */
            edit(data) {
                vm.disinfectId = data.disinfectId;
                vm.disinfectTime = data.disinfectTime;
                vm.disinfectionPlace = data.disinfectionPlace;
                vm.disinfectName = data.disinfectName;
                vm.dosage = data.dosage;
                if(data.method=="喷雾" || data.method=="熏蒸" || data.method=="紫外线" || data.method=="浸泡"){
                    // vm.method=data.method;
                    $("#method").val(data.method);
                }else{
                    // vm.method = "其它";
                    // vm.otherMethod=data.method;
                    $("#method").val("其它");
                    $("#otherMethod").val(data.method);
                    document.getElementById("A").style.display = "block";
                    document.getElementById("B").style.display = "block";
                }
                vm.method = data.method;
                vm.remark = data.remark;
                vm.clickState = "edit";
                $('#myModalAdd').modal('show');
            },
            /**
             * 删除
             * @param data
             */
            del(data) {
                swal({
                    title: "确认是否删除",
                    text: "是否确认要删除该条记录？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "删除",
                    cancelButtonText: "取消",
                    closeOnConfirm: false
                }, function () {
                    let url = "/disinfect/del";
                    let pm = {
                        disinfectId: data.disinfectId
                    };
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            swal("删除成功!", "请继续操作", "success");
                            vm.refresh();
                        } else {
                            swal("删除失败!", "请重试", "error");
                        }
                    });
                });
            },
            /**
             * 新增
             */
            addRecord() {
                vm.disinfectId = "";
                vm.disinfectTime = "";
                vm.disinfectionPlace = "";
                vm.disinfectName = "";
                vm.dosage = "";
                vm.method = "";
                vm.otherMethod="";
                vm.remark = "";
                vm.clickState = "add";
                $('#myModalAdd').modal('show');
            },
            /**
             * 保存
             */
            saveRecord() {
                if(!validateForm()) return false;
                if(vm.method=="其它"){
                    vm.method=vm.otherMethod;
                }
                let pm = {
                    disinfectId:vm.disinfectId,
                    disinfectTime:vm.disinfectTime,
                    disinfectionPlace:vm.disinfectionPlace,
                    disinfectName:vm.disinfectName,
                    dosage:vm.dosage,
                    method:vm.method,
                    remark:vm.remark,
                }
                if (vm.clickState === "edit") {
                    let url = "/disinfect/edit";
                    axios.post(url, pm).then(data => {
                        if (data.data.code === "200") {
                            $('#myModalAdd').modal('hide')
                            swal("修改成功!", "请继续操作", "success");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                        swal("修改失败!", "请重试", "error");
                    });
                } else if (vm.clickState === "add") {
                    let url = "/disinfect/add";
                    axios.post(url, pm).then(data => {
                        $('#tableList').bootstrapTable(('refresh'));
                        if (data.data.code === "200") {
                            $('#myModalAdd').modal('hide')
                            swal("添加成功!", "请继续操作", "success");
                            this.refresh();
                        }
                    }).catch(e => {
                        console.log(e);
                        swal("添加失败!", "请重试", "error");
                    });
                }
            },
            refresh() {
                $('#tableList').bootstrapTable(('refresh'));
            },
            //消毒方式值改变事件
            changeMethod(){
                if(vm.method=="其它"){
                    document.getElementById("A").style.display = "block";
                    document.getElementById("B").style.display = "block";
                }else{
                    document.getElementById("A").style.display = "none";
                    document.getElementById("B").style.display = "none";
                }
            },
        }
    });
};
//表单验证
function validateForm() {
    //validate方法参数可选
    return $("#myform").validate({
        rules: {
            disinfectTime: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            disinfectionPlace: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            disinfectName: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            dosage: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            method: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
        },
        //错误信息提示
        messages: {
            disinfectTime: {
                required: "(必填)",
                minlength: "最少1位",
            },
            disinfectionPlace: {
                required: "(必填)",
                minlength: "最少1位",
            },
            disinfectName: {
                required: "(必填)",
                minlength: "最少1位",
            },
            dosage: {
                required: "(必填)",
                minlength: "最少1位",
            },
            method: {
                required: "(必填)",
                minlength: "最少1位",
            },
        },
    }).form();
}

